//
//  SZLIncomeDetailViewController.m
//  Shengzhuli
//
//  Created by 卡忙科技 on 16/8/7.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "SZLIncomeDetailViewController.h"
#import "IncomeDetailTableViewCell.h"
#import "MyCountNetwork.h"
#import "SZLAccountHelper.h"
#import "MBProgressHUD+MJ.h"
#import <MJRefresh.h>

@interface SZLIncomeDetailViewController ()

@property (nonatomic,assign) NSInteger curPage;

@end

@implementation SZLIncomeDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"收益详情";
    [self setTableView];
    self.curPage = 1;
    [self getIncomeDetailList];
}

- (void)setTableView
{
    self.sourceTab.rowHeight = 100;
    [self.sourceTab registerNib:[UINib nibWithNibName:NSStringFromClass([IncomeDetailTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([IncomeDetailTableViewCell class])];
    __unsafe_unretained typeof(self) weakSelf = self;
    self.sourceTab.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.curPage++;
        [self getIncomeDetailList];
    }];
    self.sourceTab.tableFooterView = [[UIView alloc]init];
    [self.view addSubview:self.sourceTab];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.sources.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    IncomeDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([IncomeDetailTableViewCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.sources[indexPath.row];
    return cell;
}

- (void)getIncomeDetailList
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"pageNo"] = @(self.curPage);
    [self showWaiter];
    [MyCountNetwork getIncomeListWithParame:parame response:^(BOOL success, NSString *msg, NSArray *lists) {
        [self hideWaiter];
        if (success) {
            [self hideRequestAgainView];
            [self.sourceTab.mj_footer endRefreshing];
            self.sourceTab.mj_footer.hidden = (lists.count<20)?YES:NO;
            if (self.curPage==1) {
                if (lists.count == 0) {
                    [self showNoDataView:self.view.frame title:@"暂无收益记录"];
                    return ;
                }
            }else{
                if (lists.count == 0) {
                    self.curPage--;
                    return;
                }
            }
            [self.sources addObjectsFromArray:lists];
            [self.sourceTab reloadData];
        }else{
            if ([msg isEqualToString:NoNetworkStatus]) {
                [self showRequestAgainView:@selector(getIncomeDetailList)];
            }
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}

@end
